package com.iotek.bookstore.web.controlle;

import com.iotek.bookstore.modle.entity.Book;
import com.iotek.bookstore.modle.entity.BookContent;
import com.iotek.bookstore.modle.entity.BookDetails;
import com.iotek.bookstore.modle.service.serviceimpl.BookBizImpl;
import com.iotek.bookstore.modle.service.serviceimpl.BookContentBizImpl;
import com.iotek.bookstore.modle.service.serviceimpl.BookDetailsBizImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/login/booksDetailsServlet")
public class BookDetailsServlet extends CommonServlet {
    @Override
    public void doInsert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book>books=new BookBizImpl().queryAll();
        int bookId=books.get(books.size()-1).getBookId()+1;
        String book=req.getParameter("book");
        String writer=req.getParameter("writer");
        String press=req.getParameter("press");
        String publicationTime=req.getParameter("publicationTime");
        int titleTwoId=Integer.parseInt(req.getParameter("titleTwoId"));
        String printingTime=req.getParameter("printingTime");
        int print=Integer.parseInt(req.getParameter("print"));
        String ISBN=req.getParameter("ISBN");
        double pricing=Double.valueOf(req.getParameter("pricing"));
        double price=Double.parseDouble(req.getParameter("price"));
        int repertory=Integer.parseInt(req.getParameter("repertory"));
        int words=Integer.parseInt(req.getParameter("words"));
        int pages=Integer.parseInt(req.getParameter("pages"));
        String format=req.getParameter("format");
        String sheet=req.getParameter("sheet");
        String pack=req.getParameter("pack");
        String contentIntro=req.getParameter("contentIntro");
        String writerIntro=req.getParameter("writerIntro");
        String catalogue=req.getParameter("catalogue");
        String mediaComments=req.getParameter("mediaComments");
        String excerpt=req.getParameter("excerpt");
        String intro=req.getParameter("intro");
        String cover=(String)req.getSession().getAttribute("path");
        Book book1=new Book(bookId,titleTwoId,cover,book,writer,press,publicationTime,intro,pricing,price,repertory,null,0);
        BookContent bookContent=new BookContent(bookId,printingTime,print,ISBN,words,pages,format,sheet,pack,contentIntro,writerIntro,catalogue,mediaComments,excerpt);
        if(new BookBizImpl().insert(book1)==0||new BookContentBizImpl().insert(bookContent)==0){
            resp.getWriter().write("0");
        }else {
            resp.getWriter().write("1");
        }
    }

    @Override
    public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int bookId=Integer.parseInt(req.getParameter("bookId"));
        System.out.println("信息来自-->BookDetailsServlet--->doDelete:");
        System.out.println("图书ID："+bookId);
        if(new BookBizImpl().delete(bookId)==0||new BookContentBizImpl().delete(bookId)==0){
            resp.getWriter().write("0");
        }else {
            resp.getWriter().write("1");
        }

    }

    @Override
    public void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int bookId=Integer.parseInt(req.getParameter("bookId"));
        String book=req.getParameter("book");
        String writer=req.getParameter("writer");
        String press=req.getParameter("press");
        String publicationTime=req.getParameter("publicationTime");
        int titleTwoId=Integer.parseInt(req.getParameter("titleTwoId"));
        String printingTime=req.getParameter("printingTime");
        int print=Integer.parseInt(req.getParameter("print"));
        String ISBN=req.getParameter("ISBN");
        double pricing=Double.valueOf(req.getParameter("pricing"));
        double price=Double.parseDouble(req.getParameter("price"));
        int repertory=Integer.parseInt(req.getParameter("repertory"));
        int words=Integer.parseInt(req.getParameter("words"));
        int pages=Integer.parseInt(req.getParameter("pages"));
        String format=req.getParameter("format");
        String sheet=req.getParameter("sheet");
        String pack=req.getParameter("pack");
        String contentIntro=req.getParameter("contentIntro");
        String writerIntro=req.getParameter("writerIntro");
        String catalogue=req.getParameter("catalogue");
        String mediaComments=req.getParameter("mediaComments");
        String excerpt=req.getParameter("excerpt");
        String intro=req.getParameter("intro");
        Book book1=new Book(bookId,titleTwoId,null,book,writer,press,publicationTime,intro,pricing,price,repertory,null,0);
        BookContent bookContent=new BookContent(bookId,printingTime,print,ISBN,words,pages,format,sheet,pack,contentIntro,writerIntro,catalogue,mediaComments,excerpt);
        if(new BookBizImpl().update(book1)==0||new BookContentBizImpl().update(bookContent)==0){
            resp.getWriter().write("0");
        }else {
            resp.getWriter().write("1");
        }
    }

    @Override
    public void doQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int bookId=Integer.parseInt(req.getParameter("bookId"));
        List<BookDetails>bookDetails=new BookDetailsBizImpl().query(bookId);
        req.setAttribute("bookDetails",bookDetails);
        if(req.getParameter("type")==null){
            req.getRequestDispatcher("/booksDetails.jsp").forward(req,resp);
        }else {
            req.getRequestDispatcher("/admin/booksDetailsUpdate.jsp").forward(req,resp);
        }

    }
}
